/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dalobject;

import dalInterface.DALAccount;
import java.rmi.RemoteException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import utils.DataTable;

/**
 *
 * @author salm
 */
public class DALAccountImpl implements DALAccount {

    private Connection sqlConnection;

    public DALAccountImpl(Connection connect) throws RemoteException {
        super();
        this.sqlConnection = connect;
    }

    @Override
    public DataTable getAccount(String userName, String password) throws RemoteException
    {
        // NEED TABLE
        try {
            Statement st = sqlConnection.createStatement();
            String queryString = String.format("SELECT * FROM ACCOUNT WHERE UserID=\'%s\' AND Password=\'%s\'", userName, password);
            
            return DataTable.createDataTable(st.executeQuery(queryString), "UserID", "UserName", "Experience", "Level");
        } catch (SQLException ex) {
            Logger.getLogger(DALAccountImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return null;
    }

    @Override
    public boolean isAccountExist(String userName) throws RemoteException
    {
        try {
            Statement st = sqlConnection.createStatement();
            String queryString = String.format("SELECT Name FROM ACCOUNT WHERE Name=\'%s\'", userName);
            ResultSet rs = st.executeQuery(queryString);
            
            return rs.next();
        } catch (SQLException ex) {
            Logger.getLogger(DALAccountImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return false;
    }

    @Override
    public boolean insertAccount(String userName, String password) throws RemoteException
    {
        try {
            Statement st = sqlConnection.createStatement();
            String queryString = String.format("INSERT INTO ACCOUNT VALUES (\'%s\', \'%s\', \'%s\', 0, 1, 1)", userName, userName, password);
            
            st.execute(queryString);
            return true;
        } catch (SQLException ex) {
            Logger.getLogger(DALAccountImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return false;
    }    

    @Override
    public DataTable getAccountInfo(String userName) throws RemoteException 
    {
        try {
            Statement st = sqlConnection.createStatement();
            String queryString = String.format("SELECT * FROM ACCOUNT WHERE UserID=\'%s\'", userName);
            
            return DataTable.createDataTable(st.executeQuery(queryString), "UserID", "UserName", "Experience", "Level", "TypeID");
        } catch (SQLException ex) {
            Logger.getLogger(DALAccountImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return null;
    }
    
    public boolean changePassword( String userName, String oldPas, String newPas ) throws RemoteException
    {
        try {
            Statement st = sqlConnection.createStatement();
            String queryString = String.format("UPDATE ACCOUNT SET Password = \'%s\' WHERE UserID=\'%s\' AND Password = \'%s\'", newPas, userName, oldPas);
            
            st.executeUpdate(queryString);
            return true;
        } catch (SQLException ex) {
            Logger.getLogger(DALAccountImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return false;
    }
}
